package com.gtw.modules.sys.controller;

import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.api.service.TbUserService;
import com.gtw.modules.entity.TbUser;
import com.gtw.modules.entity.TbWalletPayLog;
import com.gtw.modules.sys.service.TbWalletPayLogService;
import com.gtw.utils.BankUtil;
import com.gtw.utils.TransferPay;
import com.gtw.utils.WithdrawStatus;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by wuye on 2018/4/2.
 */
@RestController
@RequestMapping("/sys/walletpaylog")
public class TbWalletPayLogController {

    @Autowired
    private TbWalletPayLogService walletpaylogService;

    @Autowired
    private  TransferPay transferPay;

    @Autowired
    private TbUserService tbUserService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:walletpaylog:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<TbWalletPayLog> walletpaylogList = walletpaylogService.queryList(query);
        int total = walletpaylogService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(walletpaylogList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:walletpaylog:info")
    public R info(@PathVariable("id") Integer id){
        TbWalletPayLog walletpaylog = walletpaylogService.queryObject(id);
        walletpaylog.setTotalfee(walletpaylog.getTotalfee()/100);
        walletpaylog.setTitle(walletpaylog.getTitle()+" "+" 所属银行："+ BankUtil.getNameOfBank(walletpaylog.getTitle()));
        return R.ok().put("walletpaylog", walletpaylog);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:walletpaylog:save")
    public R save(@RequestBody TbWalletPayLog walletpaylog){
        walletpaylogService.save(walletpaylog);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:walletpaylog:update")
    public R update(@RequestBody TbWalletPayLog walletpaylog){
        walletpaylogService.update(walletpaylog);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:walletpaylog:delete")
    public R delete(@RequestBody Integer[] ids){
        walletpaylogService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 批准
     * @return
     */
    @RequestMapping("/Approved")
    public R Approved(HttpServletRequest request, HttpServletResponse response, @RequestBody Integer id){
        TbWalletPayLog tbWalletPayLog = walletpaylogService.queryObject(id);
        if(tbWalletPayLog.getType().equals(4)){
            return R.error().put("msg","已经审核过了");
        }
        if(tbWalletPayLog.getType().equals(3)){
            return R.error().put("msg","已经审核过了");
        }
        tbWalletPayLog.setType(2);
        tbWalletPayLog.setPaystatus(WithdrawStatus.review_ok.getName());
        walletpaylogService.update(tbWalletPayLog);
        //打款
//        int str = transferPay.transferPay(request,response,tbWalletPayLog);
//        if(str == 1) {
            tbWalletPayLog.setType(4);
            tbWalletPayLog.setPaystatus(WithdrawStatus.success.getName());
            walletpaylogService.update(tbWalletPayLog);
//            return R.ok().put("msg","转账成功");
//        }else{
//            return R.error("转账失败");
//        }
        return R.ok();
    }

    /**
     * 拒绝
     * @return
     */
    @RequestMapping("/refuse")
    public R refuse( @RequestBody Integer id){
        TbWalletPayLog tbWalletPayLog = walletpaylogService.queryObject(id);
        if(tbWalletPayLog.getType().equals(4)){
            return R.error().put("msg","已经审核过了");
        }
        if(tbWalletPayLog.getType().equals(3)){
            return R.error().put("msg","已经审核过了");
        }
        TbUser user = tbUserService.queryByUid(tbWalletPayLog.getUid());
        if(user != null) {
            user.setBalance(user.getBalance() + tbWalletPayLog.getTotalfee());
            tbUserService.update(user);
            tbWalletPayLog.setType(3);
            tbWalletPayLog.setPaystatus(WithdrawStatus.review_no.getName());
            walletpaylogService.update(tbWalletPayLog);
        }else{
            return R.error().put("msg","数据异常");
        }

        return R.ok();
    }



}
